/*
 * Copyright 2011-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.sabayframework.channel.impl;

import org.sabayframework.channel.Channel;
import org.sabayframework.channel.Lock;

/**
 * Delegating channel is useful when you need to override default behavior of
 * an existing channel. 
 * 
 * @author Alex Antonau
 * */
public class DelegatingChannel<T> implements Channel<T> {

	private final Channel<T> delegate;

	public DelegatingChannel(Channel<T> delegate) {
		super();
		this.delegate = delegate;
	}

	public Channel<T> getDelegate() {
		return delegate;
	}

	public Lock<T> getReadLock(int size) {
		return delegate.getReadLock(size);
	}

	public Lock<T> getWriteLock(int size) {
		return delegate.getWriteLock(size);
	}

	public T remove() throws IllegalStateException {
		return delegate.remove();
	}

	public void add(T elem) throws IllegalStateException {
		delegate.add(elem);
	}

	public boolean isEmpty() {
		return delegate.isEmpty();
	}

	public boolean isFull() {
		return delegate.isFull();
	}

	public int size() {
		return delegate.size();
	}

	public int capacity() {
		return delegate.capacity();
	}

	public int freeSize() {
		return delegate.freeSize();
	}

	public void close() {
		delegate.close();
	}

	public boolean isClosed() {
		return delegate.isClosed();
	}

	public void fail(Throwable cause) {
		delegate.fail(cause);
	}

	public boolean hasFailed() {
		return delegate.hasFailed();
	}

	public Throwable getFailureCause() {
		return delegate.getFailureCause();
	}

	public Lock<T> waitForReadLock(int count, long timeout)
			throws InterruptedException {
		return delegate.waitForReadLock(count, timeout);
	}

	@Override
	public int hashCode() {
		return delegate.hashCode();
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean equals(Object obj) {
		if (this == obj) return true;
		if (obj == null) return false;
		if (getClass() == obj.getClass()){
			obj = ((DelegatingChannel<T>)obj).delegate;
		}
		return delegate.equals(obj);
	}

}
